import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/IpWeatherView.vue'

const router = createRouter({
  // 配置使用history模式
  history: createWebHistory(import.meta.env.BASE_URL),
  // 定义路由规则
  routes: [
    {
      path: '/', // 路由名称
      name: 'home',
      component: HomeView // 同步组件
    },
    {
      path: '/about/:city',
      name: 'about',
      // route level code-splitting
      // this generates a separate chunk (About.[hash].js) for this route
      // which is lazy-loaded when the route is visited.
      component: () => import('../views/SearchWeatherView.vue'), // 异步组件
      beforeEnter:(to, from, next) => {
          if(from.name === 'home'){
            next()
          }else{
            next({name: '404'})
          }
      }
    },{
      path: '/404',
      name: '404',
      component: () => import('../views/404View.vue')
    }
  ]
})


export default router